package client.comm.baoding.ui


import android.app.Activity
import android.content.Intent
import android.view.View
import androidx.core.view.children
import androidx.lifecycle.Observer
import androidx.navigation.fragment.NavHostFragment
import androidx.recyclerview.widget.LinearLayoutManager
import client.comm.commlib.toast.toast
import client.comm.commlib.widget.EmptyLayout
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.adapter.AddrMangerAdapter
import client.comm.baoding.databinding.ActivityAddrmangerBinding
import client.comm.baoding.dialog.QrDelAddrDialog
import client.comm.baoding.ui.vm.AddrMangerViewModel

class AddrMangerActivity : BaseMiddleActivity<AddrMangerViewModel, ActivityAddrmangerBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_addrmanger
    }

    val adapter: AddrMangerAdapter by lazy {
        AddrMangerAdapter(this@AddrMangerActivity).apply {
            del = {
                QrDelAddrDialog(this@AddrMangerActivity).apply {
                    qrClick = {
                        viewModel.del(it)
                    }
                }.show()
            }
            edit = {
                startActivity(
                    Intent(
                        this@AddrMangerActivity,
                        AddAddrActivity::class.java
                    ).apply {
                        putExtra("bean", it)
                        putExtra("isEdit", true)
                    })
            }
            itemClick = {
                if (intent.getBooleanExtra("select", false)) {
                    setResult(Activity.RESULT_OK, Intent().apply {
                        putExtra("address", it)
                    })
                    finish()
                }
            }
            defItem = {

                viewModel.defAddr(it)
            }
        }
    }

    override fun initViewModel(): AddrMangerViewModel = createViewModel()

    override fun initView() {
        binding.event = this
        val isSelectMode = intent.getBooleanExtra("select", false)
        if (isSelectMode) {
            binding.tabWrap.visibility = View.GONE
            val index = intent.getIntExtra("index", 0)

            when ("$index") {
                "0" -> {
                    (supportFragmentManager.findFragmentById(
                        R.id.fragment
                    ) as NavHostFragment).navController.navigate(
                        R.id.oneTab
                    )
                }
                "1" -> {
                    (supportFragmentManager.findFragmentById(
                        R.id.fragment
                    ) as NavHostFragment).navController.navigate(
                        R.id.twoTab
                    )
                }
            }

            binding.tabWrap.getChildAt(index).isSelected = true
        } else binding.tabWrap.getChildAt(0).isSelected = true
    }

    fun tab(v: View) {
        if (v.isSelected)
            return
        for (c in binding.tabWrap.children) {
            if (c != v) {
                c.isSelected = false
            } else {
                v.isSelected = true
                when (v.tag) {
                    "0" -> {
//                        if (!(supportFragmentManager.findFragmentById(
//                                R.id.fragment
//                            ) as NavHostFragment).navController.popBackStack(
//                                R.id.oneTab,
//                                false
//                            )
//                        ) {
                        (supportFragmentManager.findFragmentById(
                            R.id.fragment
                        ) as NavHostFragment).navController.navigate(
                            R.id.oneTab
                        )
//                        }
                    }

                    "1" -> {
//                        if (!(supportFragmentManager.findFragmentById(
//                                R.id.fragment
//                            ) as NavHostFragment).navController.popBackStack(
//                                R.id.twoTab,
//                                false
//                            )
//                        ) {
                        (supportFragmentManager.findFragmentById(
                            R.id.fragment
                        ) as NavHostFragment).navController.navigate(
                            R.id.twoTab
                        )
//                        }
                    }
                }
            }
        }
    }

}